package com.jss.lanouhn.fristapp.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.SelecterDetails;
import com.jss.lanouhn.fristapp.adapters.RV_RecommendAdapter;
import com.jss.lanouhn.fristapp.adapters.RvlAdapter;
import com.jss.lanouhn.fristapp.modles.SelecterSampleImgInfo;
import com.jss.lanouhn.fristapp.modles.SelecterSampleInfo;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/5.
 */
public class Fragment_Selecter_Sample extends Fragment {
    private String urlTop = "http://api.liwushuo.com/v2/items/";
    private String ImgurlEnd = "/recommend?num=20&post_num=5";
    private String url;
    private String Imgurl;
    private ImageView iv_selecter_top;
    private TextView tv_title, tv_money, tv_details;
    private SelecterSampleInfo ssinfos;
    private SelecterSampleImgInfo ssimginfos;
    private RecyclerView rv_recommend,rv_likes;
    private RV_RecommendAdapter rvcAdapter;
    private RvlAdapter rvlAdapter;
    private FrameLayout fl_recommend,fl_likes;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_selecter_sample, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String id = ((SelecterDetails) getActivity()).getId();
        Log.d("present", id);
        url = urlTop + id;
        Imgurl = url + ImgurlEnd;
        Log.d("present", url);

        initView();
        initData();
        initImage();

    }

    private void initView() {
        iv_selecter_top = (ImageView) getView().findViewById(R.id.iv_selecter_top);
        tv_title = (TextView) getView().findViewById(R.id.tv_sample_title);
        tv_money = (TextView) getView().findViewById(R.id.tv_sample_money);
        tv_details = (TextView) getView().findViewById(R.id.tv_sample_details);
        rv_recommend= (RecyclerView) getView().findViewById(R.id.rv_sample_recommend);
        rv_likes= (RecyclerView) getView().findViewById(R.id.rv_sample_likes);
        fl_recommend= (FrameLayout) getView().findViewById(R.id.fl_sample_recommend);
        fl_likes= (FrameLayout) getView().findViewById(R.id.fl_sample_likes);

    }

    private void initData() {
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request
                .Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                ssinfos = gson.fromJson(result, SelecterSampleInfo.class);
                handler.sendEmptyMessage(1);

            }
        });

    }

    private void initImage() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Imgurl)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                ssimginfos = gson.fromJson(result, SelecterSampleImgInfo.class);
                handler.sendEmptyMessage(2);

            }
        });

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    SelecterSampleInfo.DataBean bean = ssinfos.getData();
                    Picasso.with(getContext()).load(bean.getCover_image_url()).into(iv_selecter_top);
                    tv_money.setText("￥" + bean.getPrice());
                    tv_title.setText(bean.getName());
                    tv_details.setText(bean.getDescription());
                    break;
                case 2:
                    if (ssimginfos.getData().getRecommend_posts().size()>0) {
                        rvcAdapter = new RV_RecommendAdapter(ssimginfos, getContext());
                        rv_recommend.setAdapter(rvcAdapter);
                        LinearLayoutManager manager = new LinearLayoutManager(getContext());
                        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                        rv_recommend.setLayoutManager(manager);
                    }else {
                        fl_recommend.setVisibility(View.GONE);
                    }

                    if (ssimginfos.getData().getRecommend_items().size()>0){
                        rvlAdapter=new RvlAdapter(ssimginfos,getContext());
                        rv_likes.setAdapter(rvlAdapter);
                        GridLayoutManager gridLayoutManager=new GridLayoutManager(getContext(),2);
                        rv_likes.setLayoutManager(gridLayoutManager);
                    }

                    break;

            }

        }
    };


}
